package com.app.service.impl;

import com.app.model.*;
import com.app.service.*;
import com.app.util.StringTool;
import com.app.vo.SkillTrainingVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.app.mapper.SkillTrainingMapper;
import com.app.mapper.WorkApplyMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;


/**
 * @Description:投递简历记录
 * @Author: N
 * @Date: 2022年11月24日 10点29分
 */
@Service
public class SkillTrainingServiceImpl implements SkillTrainingService {

    @Autowired
    SkillTrainingMapper skillTrainingMapper;
    @Autowired
    TrainTypeConfigService trainTypeConfigService;
    @Autowired
    ConfigService configService;
    @Autowired
    TrainTypeConfigSonService trainTypeConfigSonService;
    @Override
    public int insert(SkillTrainingModel model) {
        return skillTrainingMapper.insert(model);
    }

    @Override
    public void update(SkillTrainingModel model) {
        skillTrainingMapper.updateById(model);
    }

    @Override
    public void delete(int id) {
        skillTrainingMapper.deleteById(id);
    }

    @Override
    public Page<SkillTrainingModel> getSkillTrainingList(SkillTrainingModel model) {
        QueryWrapper<SkillTrainingModel> queryWrapper = new QueryWrapper<>();
        if(model.getState()!=null&&model.getState()>0)
        {
            queryWrapper.select().eq("state", model.getState());
        }
        Page<SkillTrainingModel> page = new Page<>();
        page.setCurrent(model.getPage());
        page.setSize(model.getLimit());
        Page<SkillTrainingModel> userList = skillTrainingMapper.selectPage(page,queryWrapper);
        if (userList==null) {
            return null;
        } else {
            return userList;
        }
    }

    @Override
    public int getSkillTrainingCount(SkillTrainingModel model) {
        QueryWrapper<SkillTrainingModel> queryWrapper = new QueryWrapper<>();
        if(model.getState()!=null&&model.getState()>0)
        {
            queryWrapper.select().eq("state", model.getState());
        }
        int skillTrainingModel = skillTrainingMapper.selectCount(queryWrapper);
        return skillTrainingModel;
    }

    @Override
    public SkillTrainingModel getSkillTrainingById(int id) {
        QueryWrapper<SkillTrainingModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        List<SkillTrainingModel> skillTrainingModelList = skillTrainingMapper.selectList(queryWrapper);
        if (skillTrainingModelList==null||skillTrainingModelList.isEmpty()) {
            return null;
        } else {
            return skillTrainingModelList.get(0);
        }
    }

    @Override
    public Page<SkillTrainingModel> getModelList(SkillTrainingModel model) {
        QueryWrapper<SkillTrainingModel> queryWrapper = new QueryWrapper<>();



        if(model.getState()!=null&&model.getState()>0)
        {
            queryWrapper.eq("state",model.getState());
        }
        if(StringTool.isNotEmpty(model.getKeyWord()))
        {
            queryWrapper.and(wrapper -> wrapper.like("train_name", model.getKeyWord()).or().like("company", model.getKeyWord()).or().like("describer", model.getKeyWord()));
        }
        queryWrapper.orderByDesc("crt_time");
        Page<SkillTrainingModel> page = new Page<>();
        page.setCurrent(model.getPage());
        page.setSize(model.getLimit());
        Page<SkillTrainingModel> skillTrainingPage = skillTrainingMapper.selectPage(page,queryWrapper);
        if (skillTrainingPage==null) {
            return null;
        } else {
            return skillTrainingPage;
        }

    }

    @Override
    public int getModelCount(SkillTrainingModel model) {
        return 0;
    }

    @Override
    public SkillTrainingVo trainList(SkillTrainingModel model) {
        Page<SkillTrainingModel> modelPage = getModelList(model);
        List<SkillTrainingModel> modelList = modelPage.getRecords();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        for (int i = 0; i < modelList.size(); i++) {

            if(modelList.get(i).getState()==1)
            {
                modelList.get(i).setStateText("待开班");
            }
            if(modelList.get(i).getState()==2)
            {
                modelList.get(i).setStateText("已开班");
            }
            if(modelList.get(i).getState()==3)
            {
                modelList.get(i).setStateText("已结班");
            }
            if(modelList.get(i).getState()==4)
            {
                modelList.get(i).setStateText("已关闭");
            }
        }
        int count = (int)modelPage.getTotal();
        SkillTrainingVo vo = new SkillTrainingVo();
        vo.setCount(count);
        vo.setModelList(modelList);
        return vo;
    }

    @Override
    public Integer selectCount() {
        return skillTrainingMapper.selectCount(null);
    }

    @Override
    public Page<SkillTrainingModel> selectTrainList(Integer page1, Integer limit) {
        Page<SkillTrainingModel> page = new Page<>();
        page.setCurrent(page1);
        page.setSize(limit);
        Page<SkillTrainingModel> userList = skillTrainingMapper.selectPage(page,null);
        if (userList==null) {
            return null;
        } else {
            return userList;
        }
    }
    @Override
    public SkillTrainingVo trainRecommendation(Integer page1, Integer limit, List<Integer> integerList) {
        Page<SkillTrainingModel> page = new Page<>();
        page.setCurrent(page1);
        page.setSize(limit);
        Page<SkillTrainingModel> userList = skillTrainingMapper.selectPage(page, null);
        Set<SkillTrainingModel> list = new HashSet<>();
        for (int i1 = 0; i1 < userList.getRecords().size(); i1++) {
            List<TrainTypeConfigModel> trainTypeConfigModelList = trainTypeConfigService.getListById(userList.getRecords().get(i1).getId());
            if (trainTypeConfigModelList != null && !trainTypeConfigModelList.isEmpty()) {
                for (int i = 0; i < trainTypeConfigModelList.size(); i++) {
                    List<TrainTypeConfigSonModel> trainTypeConfigSonModelList = trainTypeConfigSonService.getListByTypeIdAndTrainId(userList.getRecords().get(i1).getId(), trainTypeConfigModelList.get(i).getType());
                    //获取一级分类名称
                    ConfigModel configModel = configService.getModelById(trainTypeConfigModelList.get(i).getType());
                    trainTypeConfigModelList.get(i).setTypeText(configModel.getName());
                    for (int j = 0; j < trainTypeConfigSonModelList.size(); j++) {
                        //获取专业
                        ConfigModel configModel2 = configService.getModelById(trainTypeConfigSonModelList.get(j).getConfigId());
                        trainTypeConfigSonModelList.get(j).setTrainIdText(userList.getRecords().get(i).getCompany());
                        trainTypeConfigSonModelList.get(j).setConfigIdText(configModel2.getName());
                        trainTypeConfigSonModelList.get(j).setTypeIdText(configModel.getName());
                        for (int i2 = 0; i2 < integerList.size(); i2++) {
                            if (configModel2.getIndustry() != null && !configModel2.getIndustry().equals("") && configModel2.getIndustry().contains(integerList.get(i2).toString())) {
                                list.add(userList.getRecords().get(i1));
                            }
                        }
                    }
                    trainTypeConfigModelList.get(i).setSonList(trainTypeConfigSonModelList);
                }
            }
        }
        List<SkillTrainingModel> list1 = new ArrayList<>(list);
        SkillTrainingVo vo = new SkillTrainingVo();
        vo.setModelList(list1);
        vo.setCount(list.size());
        return vo;
    }
}
